package com.kordar.admin.modular.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kordar.admin.enums.EnumUtils;
import com.kordar.admin.enums.ManagerStatus;
import com.kordar.admin.enums.YorN;
import com.kordar.admin.kernel.wrapper.BatchDeleteWrapperEntity;
import com.kordar.admin.kernel.wrapper.UpdateWrapperEntity;
import com.kordar.admin.modular.sys.entity.DictEntity;
import com.kordar.admin.modular.sys.entity.DictTypeEntity;
import com.kordar.admin.modular.sys.service.DictService;
import com.kordar.admin.modular.sys.service.DictTypeService;
import com.kordar.datalist.DatalistService;
import com.kordar.datalist.ResultEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

@Controller
@RequestMapping("sys")
public class DictController {

    final
    DictService dictService;

    final
    DictTypeService dictTypeService;

    final
    DatalistService datalistService;

    public DictController(DatalistService datalistService, DictService dictService, DictTypeService dictTypeService) {
        this.datalistService = datalistService;
        this.dictService = dictService;
        this.dictTypeService = dictTypeService;
    }

    @ModelAttribute("ynLabels")
    public Map<String, String> yn() {
        return EnumUtils.EnumToMapString(YorN.class);
    }

    @ModelAttribute("statusLabels")
    public Map<String, String> active() {
        return EnumUtils.EnumToMapString(ManagerStatus.class);
    }

    @ModelAttribute("pidLabels")
    public List<DictTypeEntity> pid() {
        return dictTypeService.list(new QueryWrapper<DictTypeEntity>().eq("status", ManagerStatus.OK.getCode()));
    }

    @GetMapping("dict/index")
    public String index(Model model) {
        Subject subject = SecurityUtils.getSubject();
        String tenant = (String) Optional.ofNullable(subject.getSession().getAttribute("tenant")).orElse("");
        System.out.println("****************************" + tenant);
        model.addAttribute("data", datalistService.render(dictService.list(), DictEntity.class));
        return "pages/modular/system/dict/index";
    }

    @GetMapping("dict/form")
    public String form(Model model, @RequestParam(required = false, defaultValue = "0") Long id) {
        if (id == 0) {
            model.addAttribute("header", "新建项目");
            model.addAttribute("dict", new DictEntity());
        } else {
            model.addAttribute("header", "修改项目");
            model.addAttribute("dict", dictService.getById(id));
        }

        return "pages/modular/system/dict/form";
    }

    @PostMapping("dict/save")
    @ResponseBody
    public ResultEntity<Object> save(@Validated DictEntity dictEntity) {
        if (dictEntity.getDictId() == 0) {
            dictEntity.setDictId(null);
            dictService.save(dictEntity);
        } else {
            dictService.updateById(dictEntity);
        }
        return new ResultEntity<>();
    }

    @RequestMapping("dict/edit")
    @ResponseBody
    public ResultEntity<Object> edit(@RequestBody UpdateWrapperEntity<DictEntity> entity) {
        UpdateWrapper<DictEntity> query = entity.where(DictEntity.class).data().getQuery();
        dictService.update(query);
        return new ResultEntity<>();
    }

    @PostMapping("dict/delete")
    @ResponseBody
    public ResultEntity<Object> del(long id) {
        dictService.removeById(id);
        return new ResultEntity<>();
    }

    @RequestMapping("dict/batch-delete")
    @ResponseBody
    public ResultEntity<Object> del(@RequestBody BatchDeleteWrapperEntity<Long> entity) {
        Set<Long> ids = entity.getIds();
        dictService.removeByIds(ids);
        return new ResultEntity<>();
    }

}
